/*
 * The MIT License
 *
 * Copyright 2014 David Rodenas Pico.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package edu.uoc.sd.fr14.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * This represents a sequence of operations of a server log.
 * <p>
 * 
 * It is used to get, send and apply operations through network.
 * It is obtained by Log, and applied in other servers if their are accepted.
 * 
 * @author drodenas
 */
public final class SubLog implements Serializable {
	private static final long serialVersionUID = 7859273343081182919L;
	private final List<Op> ops = new ArrayList<>();

    SubLog(List<Op> ops) {
        assert ops != null;
        
        this.ops.addAll(ops);
    }
    
    /**
     * Forces the execution of all received operations into the given company.
     * <p>
     * 
     * Executes all operations in the sublog to ensure that they are
     * applied.
     * It is possible to end with overbooking in some flight because all
     * seat operations are executed without checking if there is enough
     * space in the flight.
     * It is considered more important to ensure that payed customers have
     * confirmed reservation in the flight.
     * <p>
     * 
     * You should call to this function when you have received an
     * apply message from other server and nseq is accepted 
     * ( {@link NseqTracker#isAccepted(Nseq, ServerId)} ).
     * 
     * @param company the company of the current server
     */
    public void apply(Company company) {
        if (company == null) throw new NullPointerException("Illegal argument: company cannot be null");
        
        for (Op op : ops) {
        	// it uses forceExecute because if it was applied in other
        	// server it must be also applied here, even when there is overbooking
            op.forceExecute(company);
        }
    }

    List<Op> getOps() {
        return Collections.unmodifiableList(ops);
    }

    int size() {
        return ops.size();
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 67 * hash + Objects.hashCode(this.ops);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final SubLog other = (SubLog) obj;
        return Objects.equals(this.ops, other.ops);
    }
    
    
    
    @Override
    public String toString() {
        return "SubLog"+ops;
    }
    
}
